package guoanmaker.provider.business.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.provider.business.model.ProvideSpacefacilities;
import guoanmaker.provider.business.service.ProviderSpacefacilitiesServiceInterface;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import guoanmaker.provider.helper.common.util.OSSUnitHelper;
import io.swagger.annotations.ApiOperation;

/**
 * Created by Light on 2017/6/22.
 */
@RestController
@RequestMapping(ApiController.SERVER_CLASS_URL)
public class ProviderSpacefacilitiesController {

	
	@Autowired
    public ProviderSpacefacilitiesController(ProviderSpacefacilitiesServiceInterface providerSpacefacilitiesServiceInterface) {
        this.providerSpacefacilitiesServiceInterface = providerSpacefacilitiesServiceInterface;
    }
	
	@Autowired
	private ProviderSpacefacilitiesServiceInterface providerSpacefacilitiesServiceInterface;
	
	/**
	 * 查询所有的空间配套设施
	 * @return 
	 */
	@ApiOperation(value="查询所有的空间配套设施", notes="查询所有的空间配套设施")
	@PostMapping("/selectAllSpacefacilities")
	public  AjaxJson selectAllSpacefacilities(){
		AjaxJson ajaxJson=new AjaxJson();
		ajaxJson.setSuccess(true).setStatus(200).setData(providerSpacefacilitiesServiceInterface.selectAllSpacefacilities());
		return ajaxJson;
	} 
	
	/**
	 * 
	 * @param spacefacilitiesNo
	 * @param spacefacilitiesYes
	 * @param spacefacilitiesName
	 * @return
	 */
	@ApiOperation(value="查询所有的空间配套设施", notes="查询所有的空间配套设施")
	@PostMapping("/insertSpacefacilities")
	public  AjaxJson insertSpacefacilities(MultipartFile spacefacilitiesNo,MultipartFile spacefacilitiesYes,MultipartFile spacefacilitiesapp,String spacefacilitiesName){
		AjaxJson ajaxJson=new AjaxJson();
		OSSUnitHelper ossunit=new OSSUnitHelper();
			OSSClient clientNo=OSSUnitHelper.getOSSClient();
			String diskName = "datas/room/";
	        String md5keyNo = OSSUnitHelper.uploadObject2OSS(clientNo, spacefacilitiesNo, diskName);
	        String urlNo=OSSUnitHelper.getUrl(clientNo, diskName, spacefacilitiesNo.getOriginalFilename());
	        if(urlNo==null&&urlNo.equals("")){
	        	ajaxJson.setSuccess(true).setStatus(250).setData("上传失败");
	        	return ajaxJson;
	        }
	        OSSClient clientYes=OSSUnitHelper.getOSSClient();
	        String md5keyYes = OSSUnitHelper.uploadObject2OSS(clientYes, spacefacilitiesYes, diskName);
	        String urlYes=OSSUnitHelper.getUrl(clientYes, diskName, spacefacilitiesYes.getOriginalFilename());
	        System.out.println(urlYes);
	        
	        OSSClient clientapp=OSSUnitHelper.getOSSClient();
	        String md5keyapp = OSSUnitHelper.uploadObject2OSS(clientapp, spacefacilitiesapp, diskName);
	        String appUrl=OSSUnitHelper.getUrl(clientapp, diskName, spacefacilitiesapp.getOriginalFilename());
	        System.out.println(appUrl);
	        
	        if(urlYes!=null&&!urlYes.equals("")){
	        	ProvideSpacefacilities provideSpacefacilities=new ProvideSpacefacilities();
	        	provideSpacefacilities.setSpacefacilitiesName(spacefacilitiesName);
	        	provideSpacefacilities.setSpacefacilitiesNoUrl(urlNo);
	        	provideSpacefacilities.setSpacefacilitiesYesUrl(urlYes);
	        	provideSpacefacilities.setSpacefacilitiesAppUrl(appUrl);
	        	try {
	        		provideSpacefacilities=providerSpacefacilitiesServiceInterface.insertSpacefacilities(provideSpacefacilities);
				} catch (Exception e) {
					// TODO: handle exception
				}
	        	ajaxJson.setSuccess(true).setStatus(200).setData(provideSpacefacilities);
	        }
	        else{
	        	ajaxJson.setSuccess(true).setStatus(250).setData("上传失败");
	        	return ajaxJson;
	        }
		return ajaxJson;
	} 
	
	@ApiOperation(value="根据配套设施的多个id串查询配套设施", notes="根据配套设施的多个id串查询配套设施")
	@PostMapping("/selectSpacefacilitiesByIds")
	public  AjaxJson selectSpacefacilitiesByIds(String ids){
		AjaxJson ajaxJson=new AjaxJson();
		try {
			ajaxJson.setSuccess(true).setStatus(200).setData(providerSpacefacilitiesServiceInterface.selectSpacefacilitiesByIds(ids));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	}
}
